﻿using System;
using KillBill.Business.Entities;
using KillBill.Presentation.Common;
using KillBill.Presentation.Modules.Administration.AdministrationServiceReference;
using KillBill.Presentation.Modules.Administration.IView;
using KillBill.Services.DTO;


namespace KillBill.Presentation.Modules.Administration.Presenter
{
    public class CustomerEditPresenter
    {
        private AdministrationServiceClient _service;
        private string _sessionId;
        public Customer Customer { get { return _customer; } }
        private readonly Customer _customer = new Customer();
        public  ICustomerEdit View{ get; set;}
        
        public CustomerEditPresenter(ICustomerEdit icustomerEdit)
        {
            View = icustomerEdit;
            _sessionId = Context.SessionID;
            _service = new AdministrationServiceClient();
        }
        public CustomerEditPresenter(Customer customer, ICustomerEdit icustomerEdit)
        {
            _customer = customer;
            View = icustomerEdit;
            _service = new AdministrationServiceClient();
            MapView();
        }
        
        private void MapCustomer()
        {
            _customer.Person = new Person();
            _customer.Person.Address = View.Address;
            _customer.Person.Email = View.Email;
            _customer.Person.FirstName = View.FirstName;
            _customer.Person.LastName = View.LastName;
            _customer.Person.PersonID = View.CustomerID;
            _customer.Person.Phone = View.Phone;
            _customer.CustomerID = View.CustomerID;
            _customer.CustomerRUC = View.CustomerRUC;
            _customer.IsActive = View.IsActive;
            _customer.Reference = View.Reference;
        }

        public void MapView()
        {
            View.Address = _customer.Person.Address;
            View.CustomerID = _customer.Person.PersonID;
            View.CustomerRUC = _customer.CustomerRUC;
            View.Email = _customer.Person.Email;
            View.FirstName = _customer.Person.FirstName;
            View.IsActive = _customer.IsActive;
            View.LastName = _customer.Person.LastName;
            View.Phone = _customer.Person.Phone;
            View.Reference = _customer.Reference;
        }

        public void SaveCustomer()
        {
            try
            {
                MapCustomer();
                KillBillDTO c = _service.AddCustomer(_customer, _sessionId);
                if (c.Success)
                {
                    View.ShowMessage(c.Messages[0]);
                }
                else
                {
                    View.ShowMessage(c.Messages[0] + " " + c.Messages[1]);
                }
            }
            catch (Exception)
            {
                View.ShowConnectionErrorMessage("Administración : Clientes");
            }
        }

        public void EditCustomer()
        {
            try
            {
                MapCustomer();
                KillBillDTO outDTO = _service.UpdateCustomer(_customer,_sessionId);
                if (!outDTO.Success)
                    if (outDTO.Messages != null && outDTO.Messages.Count > 0)
                        View.ShowMessage(outDTO.Messages[0]);
            }
            catch (Exception)
            {
                View.ShowConnectionErrorMessage("Administración : Clientes");
            }
            
        }
    }
}
